//
//  InfoListRowCell.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/23.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit

class InfoListRowItemView: UIView {

    struct Config {
        let scales: [CGFloat]
    }

    var texts: [String] = [] {
        didSet {
            var newTexts = texts
            while newTexts.count < 9 {
                newTexts.append("")
            }
            for (index, text) in newTexts.enumerated() {
                labels[index].text = text
            }
        }
    }

    let config = Config(scales: [1, 5, 5, 5, 3, 2, 3, 4, 1])

    lazy var labels: [UILabel] = {
        let labels = self.config.scales.map({ (_) -> UILabel in
            let label = UILabel()
            label.font = UIFont.systemFont(ofSize: 12)
            label.backgroundColor = UIColor.random()
            label.textColor = .lightGray
            label.textAlignment = .center
            return label
        })
        return labels
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        let amount = config.scales.reduce(0) { (result, value) in
            return result + value
        }
        for (index, label) in labels.enumerated() {
            addSubview(label)
            let scale = config.scales[index] / amount
            label.mas_makeConstraints({ (make) in
                make?.top.bottom().equalTo()(self)
                make?.width.equalTo()(self)?.multipliedBy()(scale)
                if index == 0 {
                    make?.left.equalTo()(self)
                } else {
                    make?.left.equalTo()(labels[index - 1].mas_right)
                }
            })
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
